package ao

import (
	"context"
	compb "gitee.com/micro-services/protocol/common"
	pb "gitee.com/micro-services/protocol/user/server"
	"micro.user.action.server/common"
	"micro.user.action.server/do"
	"micro.user.action.server/impl"
	"micro.user.action.server/integration"
)

func AdminUserRegistry(ctx context.Context, r *pb.AdminUserRegistryReq, w *pb.AdminUserRegistryRsp) error {
	factory := impl.AdminUserBuilder()
	userID, err := factory.UserRegistry(do.ConvertAdminUserDBData(r))
	if err != nil {
		return err
	}

	// 生成JWT
	rsp, err := integration.GenerateToken(ctx, &pb.GenerateTokenReq{
		UserID:   userID,
		UserName: r.UserName,
		Phone:    r.Phone,
		Email:    r.Email,
		HotelID:  r.HotelID,
		Platform: compb.UserPlatFormType_ADMIN_USER,
	})
	if err != nil {
		return err
	}

	w.RetCode = common.Code
	w.Msg = common.Success
	w.Expiration = rsp.Expiration
	w.Token = rsp.Token

	return nil
}

func AdminUserLogin(ctx context.Context, r *pb.AdminUserLoginReq, w *pb.AdminUserLoginRsp) error {
	factory := impl.AdminUserBuilder()
	info, err := factory.UserLogin(r.UserName, r.PassWord)
	if err != nil {
		return err
	}

	// 生成JWT
	rsp, err := integration.GenerateToken(ctx, &pb.GenerateTokenReq{
		UserID:   info.UserId,
		UserName: info.UserName,
		Phone:    info.Phone,
		Email:    info.Email,
		Platform: compb.UserPlatFormType_GENERAL_USER,
	})
	if err != nil {
		return err
	}

	w.RetCode = common.Code
	w.Msg = common.Success
	w.Expiration = rsp.Expiration
	w.Token = rsp.Token

	return nil
}

func AdminUserInfo(ctx context.Context, r *pb.AdminUserInfoReq, w *pb.AdminUserInfoRsp) error {
	factory := impl.AdminUserBuilder()
	info, err := factory.QueryUserInfo(r.UserID)
	if err != nil {
		return err
	}

	w.RetCode = common.Code
	w.Msg = common.Success
	w.UserInfo = do.ConvertAdminUserInfo(info)

	return nil
}
